﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntLibEC.Biz;
using EntLibEC.Cmn;
using System.Collections;
using System.Data;
using EntLibEC.Biz.Online;

namespace EntLibEC.View.Online
{
    public partial class OnlineListAreaSearch : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btnAdd.Attributes["onclick"] = "javascript:openWindowS2('onlinelistareaopt.aspx?opt=new');return false;";

            if (!Page.IsPostBack)
            {
                ucCategory.BindC1(true);
                ucPagePosition.BindPagePosition(true);
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (!checkForm())
            {
                return;
            }

            BindGrid();
        }

        private bool checkForm()
        {
            if (txtListAreaKey.Text.Trim().Length > 0 && !Util.IsNaturalNumber(txtListAreaKey.Text.Trim()))
            {
                Assert(lblMessage, "显示区域主键必须为正整数。", -1);
                return false;
            }
            return true;
        }

        private void BindGrid()
        {
            Hashtable ht = new Hashtable(10);

            if(txtListAreaName.Text.Trim().Length > 0)
                ht.Add("ListAreaName", txtListAreaName.Text.Trim());
            if (txtListAreaKey.Text.Trim().Length > 0)
                ht.Add("ListAreaKey", txtListAreaKey.Text.Trim());
            if (ucCategory.C1SysNo != AppConst.IntNull)
                ht.Add("Category1SysNo", ucCategory.C1SysNo);
            if (ucPagePosition.PagePositionSysNo != AppConst.IntNull)
                ht.Add("PagePosition", ucPagePosition.PagePositionSysNo);
            if (txtListOrder.Text.Trim() != "")
                ht.Add("ListOrder", txtListOrder.Text.Trim());

            DataSet ds = ConvertDs(OnlineListAreaManager.GetInstance().GetOnlineListAreaDs(ht));

            GridView1.DataSource = ds;
            GridView1.DataBind();

            int rows = 0;
            if (Util.HasMoreRow(ds))
                rows = ds.Tables[0].Rows.Count;
            Assert(lblMessage, rows.ToString() + " record(s)", 1);

        }

        private DataSet ConvertDs(DataSet ds)
        {
            if (!Util.HasMoreRow(ds))
                return ds;

            ds.Tables[0].Columns.Add("Operation");
            ds.Tables[0].Columns.Add("PagePositionName");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                int pagePosition = Util.TrimIntNull(dr["PagePosition"]);
                string pagePositionName = AppEnum.GetPagePosition(Util.TrimIntNull(dr["PagePosition"]));
                dr["PagePositionName"] = pagePositionName;

                string operation = "<a href=\"javascript:openWindowS2('OnlineListAreaOpt.aspx?sysno=" + dr["sysno"].ToString() + "&opt=update')\">更新</a>";

                operation += "&nbsp;&nbsp;<a href=\"javascript:openWindowS2('OnlineListAreaOpt.aspx?sysno=" + dr["sysno"].ToString() + "&opt=delete')\">删除</a>";

                dr["operation"] = operation;
            }
            return ds;
        }
    }
}
